
#include "tx_api.h"
#include <stdio.h>

#include "lwip/init.h"
#include "lwip/netif.h"
#include "ethernetif.h"
#include <string.h>
#include "tcpip.h"


/* Define the ThreadX object control blocks...  */
#define THREAD_STACK_SIZE	1024
const static char *thread_name = "NET";
static TX_THREAD        thread_handle;
static UCHAR            thread_stack[THREAD_STACK_SIZE];


#define NETFIFO_SIZE 4096
TX_BYTE_POOL net_mempool;
static UCHAR  net_mempool_mem[NETFIFO_SIZE];


/*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
 #define IP_ADDR0                    10
 #define IP_ADDR1                    12
 #define IP_ADDR2                    59
 #define IP_ADDR3                    11

 /*NETMASK*/
 #define NETMASK_ADDR0               255
 #define NETMASK_ADDR1               255
 #define NETMASK_ADDR2               255
 #define NETMASK_ADDR3                 0

 /*Gateway Address*/
 #define GW_ADDR0                    10
 #define GW_ADDR1                    12
 #define GW_ADDR2                    59
 #define GW_ADDR3                    254
 /* USER CODE END 0 */



 struct netif gnetif;
 ip4_addr_t ipaddr;
 ip4_addr_t netmask;
 ip4_addr_t gw;
 uint8_t IP_ADDRESS[4];
 uint8_t NETMASK_ADDRESS[4];
 uint8_t GATEWAY_ADDRESS[4];


 void LwIP_Init(void)
 {

     tcpip_init(NULL, NULL);

     /* IP addresses initialization */
     /* USER CODE BEGIN 0 */
 #ifdef USE_DHCP
     ip_addr_set_zero_ip4(&ipaddr);
     ip_addr_set_zero_ip4(&netmask);
     ip_addr_set_zero_ip4(&gw);
 #else
     IP4_ADDR(&ipaddr,IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);
     IP4_ADDR(&netmask,NETMASK_ADDR0,NETMASK_ADDR1,NETMASK_ADDR2,NETMASK_ADDR3);
     IP4_ADDR(&gw,GW_ADDR0,GW_ADDR1,GW_ADDR2,GW_ADDR3);
 #endif /* USE_DHCP */
     /* USER CODE END 0 */
     /* Initilialize the LwIP stack without RTOS */
     /* add the network interface (IPv4/IPv6) without RTOS */
     netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL,
             &ethernetif_init, &tcpip_input);

     /* Registers the default network interface */
     netif_set_default(&gnetif);
     if (netif_is_link_up(&gnetif))
     {
     /* When the netif is fully configured this function must be called */
         netif_set_up(&gnetif);
     }
     else
     {
         /* When the netif link is down this function must be called */
         netif_set_down(&gnetif);
     }

     /* Set the link callback function, this function is called on change of link status*/
    //netif_set_link_callback(&gnetif, ethernetif_update_config);
}


 static void thread_entry(ULONG thread_input)
 {

	 LwIP_Init();

     while(1)
     {
         tx_thread_sleep(1000);
     }
 }

void thread_net_create(void)
{
	UINT status;
	tx_byte_pool_create(&net_mempool, "net pool", net_mempool_mem, sizeof(net_mempool_mem));
	status= tx_thread_create(&thread_handle,
					(CHAR *)thread_name,
					thread_entry,
					0,
					thread_stack,
					THREAD_STACK_SIZE,
					0,
					0,
					TX_NO_TIME_SLICE,
					TX_AUTO_START);
	printf("create thread %s==>[%d]!\r\n",thread_name,status);

}
